﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;
namespace CuaHangLaptop.DAL
{
    class CTHoaDon_DAL
    {
        internal static int Get_Lasted_ID()
        {
            int id = 0;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select id from ChiTietHoaDon order by id desc", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                id = rdr.GetInt32(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return id;
        }

        // SELECT * FROM NHA SAN XUAT
        internal static List<CTHoaDon_BIZ> GetAll_CTHD()
        {
            List<CTHoaDon_BIZ> result = new List<CTHoaDon_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from ChiTietHoaDon", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    CTHoaDon_BIZ dd = new CTHoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // LẤY NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static CTHoaDon_BIZ Get_CTHoaDon_ByCode(string _MaCTHD)
        {
            CTHoaDon_BIZ cthd1 = new CTHoaDon_BIZ();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from ChiTietHoaDon where MaCTHoaDon='{0}'", _MaCTHD);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                cthd1 = new CTHoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));                    
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return cthd1;
        }

        // LẤY TẤT CẢ NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static List<CTHoaDon_BIZ> GetAll_CTHoaDon_ByHD(string _MaHD)
        {
            List<CTHoaDon_BIZ> result = new List<CTHoaDon_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from ChiTietHoaDon where MaHoaDon='{0}'", _MaHD);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    CTHoaDon_BIZ dd = new CTHoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // DELETE
        internal static void Delete(CTHoaDon_BIZ cthd)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from ChiTietHoaDon where id={0}", cthd.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // UPDATE
        internal static void Update(CTHoaDon_BIZ cthd)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update ChiTietHoaDon set MaCTHoaDon = '{0}', MaHoaDon = '{1}', MaLapTop = '{2}', SoLuong = {3} where id={4}", cthd.MaCThoadon, cthd.Hoadon.MaHoaDon, cthd.Laptop.MaLaptop, cthd.Soluong, cthd.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // INSERT
        internal static int Insert(CTHoaDon_BIZ cthd)
        {
            SqlConnection conn = Connection.Connection_String();
            int newId = -1;
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("insert into ChiTietHoaDon values('{0}', '{1}','{2}',{3})", cthd.MaCThoadon, cthd.Hoadon.MaHoaDon, cthd.Laptop.MaLaptop, cthd.Soluong);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    s = String.Format("select id from ChiTietHoaDon where MaCTHoaDOn='{0}'", cthd.MaCThoadon);
                    cmd.CommandText = s;
                    rdr = cmd.ExecuteReader();

                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        newId = Int32.Parse(rdr["id"].ToString());
                    }
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return newId;
        }
    }
}
